package com.dc.commons.jpa.listeners;

import javax.persistence.PrePersist;

import com.dc.commons.helper.DateHelper;
import com.dc.commons.jpa.TicketWapper;
import com.dc.commons.persistence.able.IDAble;
import com.dc.commons.persistence.able.OperatorAble;
import com.dc.commons.persistence.able.UseAble;
import com.dc.commons.persistence.annocation.DefaultStatus;
import com.dc.commons.spring.SpringBeanFactory;
import com.dc.commons.status.UseStatus;
import com.dc.commons.utils.AnnotationUtil;

/*******************************************************************************<P>
 * 文件名: UseJpaListener.java <br>
 * 版本: 1.0<br>
 * 描述:<br>
 * 版权所有: <br>
 * <P>
 * 创建者: 王建栋 <br>
 * 创建日期: 2016年6月8日 下午3:50:06<br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
public class UseJpaListener {

	@PrePersist
	public void prepersist(IDAble idAble){
		if(idAble instanceof OperatorAble){
			UseAble useAble=(UseAble) idAble;
			UseStatus useStatus=useAble.getUseStatus();
			if(useStatus==null){
				DefaultStatus defaultStatus=AnnotationUtil.getAnnotation(useAble.getClass(), DefaultStatus.class);
				if(defaultStatus!=null){
					useStatus=defaultStatus.useStatus();
				}
				TicketWapper ticketWapper=SpringBeanFactory.getBean(TicketWapper.class);
				useAble.setUseKey(ticketWapper.getMember());
				useAble.setUseStatus(useStatus);
				useAble.setUseDate(DateHelper.newDate());
			}
		}
		
	}
}
